﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Input;
using ProjetCom.Vue;

namespace ProjetCom.Ctrl
{
    class CtrlMenu : CtrlTemplate
    {
        private Keys[] m_KonamiCode;
        private Keys[] m_MyCode;

        private int m_IndiceMyCode;

        private KeyboardState m_LastKeyboardState;
        private KeyboardState m_CurrentKeyboardState;

        public CtrlMenu(IServiceProvider p_serviceProvider, int p_score) :
                            base(p_serviceProvider, p_score) {
            m_Score = p_score;

            // haut, haut, bas, bas, gauche, droite, gauche, droite, b, a
            m_KonamiCode = new Keys[10];
            m_KonamiCode[0] = Keys.Up;
            m_KonamiCode[1] = Keys.Up;
            m_KonamiCode[2] = Keys.Down;
            m_KonamiCode[3] = Keys.Down;
            m_KonamiCode[4] = Keys.Left;
            m_KonamiCode[5] = Keys.Right;
            m_KonamiCode[6] = Keys.Left;
            m_KonamiCode[7] = Keys.Right;
            m_KonamiCode[8] = Keys.B;
            m_KonamiCode[9] = Keys.A;

            InitMyCode();

            m_Level = new LevelMenu(p_serviceProvider, this);
        }


        public override void Update(Microsoft.Xna.Framework.GameTime _gameTime) { }

        override public void EventClavier()
        {
            m_LastKeyboardState = m_CurrentKeyboardState;
            m_CurrentKeyboardState = Keyboard.GetState();

            // Test de la touche
            if (m_LastKeyboardState.IsKeyUp(Keys.Up) && m_CurrentKeyboardState.IsKeyDown(Keys.Up))
            {
                m_MyCode[m_IndiceMyCode++] = Keys.Up;
            }
            else if (m_LastKeyboardState.IsKeyUp(Keys.Down) && m_CurrentKeyboardState.IsKeyDown(Keys.Down))
            {
                m_MyCode[m_IndiceMyCode++] = Keys.Down;
            }
            else if (m_LastKeyboardState.IsKeyUp(Keys.Left) && m_CurrentKeyboardState.IsKeyDown(Keys.Left))
            {
                m_MyCode[m_IndiceMyCode++] = Keys.Left;
            }
            else if (m_LastKeyboardState.IsKeyUp(Keys.Right) && m_CurrentKeyboardState.IsKeyDown(Keys.Right))
            {
                m_MyCode[m_IndiceMyCode++] = Keys.Right;
            }
            else if (m_LastKeyboardState.IsKeyUp(Keys.A) && m_CurrentKeyboardState.IsKeyDown(Keys.A))
            {
                m_MyCode[m_IndiceMyCode++] = Keys.A;
            }
            else if (m_LastKeyboardState.IsKeyUp(Keys.B) && m_CurrentKeyboardState.IsKeyDown(Keys.B))
            {
                m_MyCode[m_IndiceMyCode++] = Keys.B;
            }
                // Si on a pressé une autre touche
            else if (m_LastKeyboardState.GetPressedKeys().Length < m_CurrentKeyboardState.GetPressedKeys().Length )
            {
                InitMyCode();
            }

            // Test du code konami
            if (IsKonamiCode())
            {
                InitMyCode();
                m_Score += 1000;
            }
        }


        private void InitMyCode()
        {
            m_MyCode = new Keys[10];
            m_IndiceMyCode = 0;
        }

        public Boolean IsKonamiCode()
        {
            for (int i = 0; i < m_IndiceMyCode; i++)
            {
                // Si il y a une erreur, on recommence la saisie
                if (m_KonamiCode[i] != m_MyCode[i])
                {
                    InitMyCode();
                    return false;
                }
            }

            // On regarde si on la combinaison correcte conserne bien 10 touches
            return m_IndiceMyCode == 10;
        }
    }
}
